using NUnit.Framework;
using TDUnit.Common.TestData;

namespace TDUnit.Tests
{
    [TestFixture]
    public class ColumnElementParserTests : XmlTestDataParserBase
    {
        #region Parse columns tests
        [Test]
        public void ColumnsCanBeParsedFromRowAttributes()
        {
            var dataset =
                GetDataSetForRow("<Row Name='RowName' ColumnOne='ColumnOneValue' ColumnTwo='ColumnTwoValue' />");
            var ds = Parse(dataset);
            var row = ds.Tables[0].Rows[0];
            Assert.IsNotNull(row.GetColumn("ColumnOne"), "ColumnOne");
            Assert.IsNotNull(row.GetColumn("ColumnTwo"), "ColumnTwo");
            Assert.AreEqual("ColumnOneValue", row.GetColumn("ColumnOne").Value);
            Assert.AreEqual("ColumnTwoValue", row.GetColumn("ColumnTwo").Value);
        }

        [Test]
        public void ParseColumnSetsPrimaryKeyColumnToValueAsRowAttribute()
        {
            var dataset =
                GetDataSetForRow("<Row Name='RowName' PrimaryKey='PKValue' />");
            var ds = Parse(dataset);
            var row = ds.Tables[0].Rows[0];
            Assert.AreEqual("PKValue", row.PrimaryKeyColumn.Value);
        }

        [Test]
        public void ColumnsCanBeParsedFromColumnElements()
        {
            var dataset =
                GetDataSetForRow("<Row Name='RowName' PrimaryKey='PKValue'><Column Name='ColumnOne' Value='Value' ShouldQuote='False'/></Row>");
            var ds = Parse(dataset);
            var row = ds.Tables[0].Rows[0];
            Assert.IsNotNull(row.GetColumn("ColumnOne"));
            Assert.AreEqual("ColumnOne", row.GetColumn("ColumnOne").Name);
            Assert.AreEqual("Value", row.GetColumn("ColumnOne").Value);
            Assert.IsFalse(row.GetColumn("ColumnOne").ShouldQuote);
        }

        [Test]
        public void ParseColumnSetsPrimaryKeyColumnToValueAsColumnElement()
        {
            var dataset =
                GetDataSetForRow("<Row Name='RowName' ><Column Name='PrimaryKey' Value='PKValue' /></Row>");
            var ds = Parse(dataset);
            var row = ds.Tables[0].Rows[0];
            Assert.AreEqual("PKValue", row.PrimaryKeyColumn.Value);
        }

        #endregion

        #region Parse DependencyColumn tests

        [Test]
        public void DependencyColumnIsSetCorrectlyToPrimaryKeyColumn()
        {
            var dataset =
                @"
                    <TestData>
                        <Table Name='Order' PrimaryKey='ID'>
                            <Row Name='OrderRow' />
                        </Table>
                        <Table Name='OrderLine' PrimaryKey='ID'>
                            <Row Name='OrderRow' OrderID='$(Order.OrderRow.ID)' />
                        </Table>
                    </TestData>
                ";

            var ds = Parse(dataset);
            ds.Tables[0].Rows[0].PrimaryKeyColumn.Value = "1";
            var row = ds.Tables[1].Rows[0];

            Assert.IsTrue(row.Columns[0] is DependencyColumn);
            Assert.AreEqual("1", row.Columns[0].Value);
        }

        [Test]
        public void DependencyColumnIsSetCorrectlyToColumn()
        {
            var dataset =
                @"
                    <TestData>
                        <Table Name='Order' PrimaryKey='ID'>
                            <Row Name='OrderRow'>
                                <Column Name='Price' Value='12.95' />
                            </Row>
                        </Table>
                        <Table Name='OrderLine' PrimaryKey='ID'>
                            <Row Name='OrderRow' OrderPrice='$(Order.OrderRow.Price)' />
                        </Table>
                    </TestData>
                ";

            var ds = Parse(dataset);
            var row = ds.Tables[1].Rows[0];
            Assert.IsTrue(row.Columns[0] is DependencyColumn);
            Assert.AreEqual("12.95", row.Columns[0].Value);
        }

        #endregion
    }
}